package com.yuxinmuyu.sports_management.exception;

/**
 * 返回对象
 *
 * @author Oven
 */
public class R<T> {

    private static final int RES_CODE_SUCCESS = 200;

    private static final String RES_CODE_SUCCESS_MSG = "成功";

    private static final int RES_CODE_FAIL = 500;

    private static final String RES_CODE_FAIL_MSG = "系统异常，请稍后重试";

    /**
     * 应答码
     */
    private Integer code;

    /**
     * 应答说明
     */
    private String msg;

    /**
     * 响应数据
     */
    private T data;

    public R() {
    }

    public R(Integer code, String msg) {
        this.code = code;
        this.msg = msg;
    }

    public static R<?> ret(boolean b) {
        return b ? success() : fail();
    }

    public static R<?> success() {
        return new R<>(RES_CODE_SUCCESS, RES_CODE_SUCCESS_MSG);
    }

    public static <T> R<T> success(T data) {
        R<T> res = new R<>(RES_CODE_SUCCESS, RES_CODE_SUCCESS_MSG);
        res.setData(data);
        return res;
    }

    public static <T> R<T> success(String msg, T data) {
        R<T> res = new R<>(RES_CODE_SUCCESS, msg);
        res.setData(data);
        return res;
    }

    public static <T> R<T> fail() {
        return fail(RES_CODE_FAIL_MSG);
    }

    public static <T> R<T> fail(String msg) {
        return new R<>(RES_CODE_FAIL, msg);
    }

    public static <T> R<T> fail(int code, String msg) {
        return new R<>(code, msg);
    }

    public Integer getCode() {
        return code;
    }

    public void setCode(Integer code) {
        this.code = code;
    }

    public String getMsg() {
        return msg;
    }

    public void setMsg(String msg) {
        this.msg = msg;
    }

    public T getData() {
        return data;
    }

    public void setData(T data) {
        this.data = data;
    }
}
